<template>
  <div class="search">
    <div class="title">
      <i class="iconfont icon-xiangzuo" @click="$router.go(-1), change()"></i>
      <div class="story">
        <input
          v-model="search"
          type="text"
          placeholder="请输入书名、作者或主要..."
        />
        <i class="iconfont icon-sousuo"></i>
      </div>
      <div class="icon" @click="add(search)">搜索</div>
    </div>

    <div v-show="!searchRes">
      <div class="searchContent" v-show="!search">
        <div class="history" v-show="historyData.length">
          <div class="searchHead">
            <h1>搜索历史</h1>
            <i class="iconfont icon-lajitong" @click="del"></i>
          </div>
          <ul>
            <li v-for="(item, index) in historyData" :key="index">
              {{ item }}
            </li>
          </ul>
        </div>
        <div class="hot">
          <h1>热门搜索</h1>
          <div class="hotWord">
            <div class="item fire" @click="forward">
              <i class="iconfont icon-huo"></i>
              雪中悍刀行
            </div>
            <div class="item fire" @click="forward">
              <i class="iconfont icon-huo"></i>
              两眼协议
            </div>
            <div class="item" @click="forward">陆少的银魂最气</div>
            <div class="item" @click="forward">镇国肾虚</div>
            <div class="item" @click="forward">七零王府小媳妇</div>
            <div class="item" @click="forward">一件读准</div>
          </div>
        </div>

        <div class="hotBooks">
          <h1>热搜书籍</h1>
          <ul>
            <li
              v-for="(item, index) in hotList"
              :key="item.id"
              @click="
                $router.push({ name: 'synopsis', query: { id: item.id } })
              "
            >
              <h1>{{ index + 1 }}</h1>
              <div class="hotText">
                <div class="img">
                  <img :src="item.img" />
                </div>
                <p>{{ item.title }}</p>
              </div>
            </li>
          </ul>
        </div>
      </div>

      <!-- 遮罩 -->
      <div class="searchKeyword" v-show="search">
        <ul>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大主宰</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大邪佛</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大奉打更人</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大王饶命</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大道朝天</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大佬每天在撒糖</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大周皇族</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
          <li @click="forward">
            <i class="iconfont icon-sousuo"></i>
            <div class="item">
              <p>大道争锋</p>
              <i class="iconfont icon-gengduo"></i>
            </div>
          </li>
        </ul>
      </div>
    </div>

    <!-- 搜索结果 -->
    <div class="searchRes" v-show="searchRes">
      <ul>
        <li
          v-for="item in hotList"
          :key="item.id"
          @click="$router.push({ name: 'synopsis', query: { id: item.id } })"
        >
          <div class="img">
            <img :src="item.img" />
          </div>
          <div class="content">
            <h1>{{ item.title }}</h1>
            <p>
              {{ item.synopsis }}
            </p>
            <span> {{ item.writer }}· {{ item.number }}</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getRecommendRank } from "../../api/index";
import { Dialog } from "vant";
export default {
  data() {
    return {
      search: null,
      searchRes: false,
      historyData: [],
      hotList: [], // 热搜列表
    };
  },
  methods: {
    change() {
      this.$emit("getnavs-btn", true);
    },
    add(obj) {
      if (this.search == null) {
        return;
      } else {
        this.historyData.push(obj);
        localStorage.setItem("search", JSON.stringify(this.historyData));
      }
      this.searchRes = true;
    },
    forward() {
      this.searchRes = true;
    },
    del() {
      Dialog.confirm({
        message: "确定删除所有搜索历史",
        confirmButtonColor: "#23B382",
      })
        .then(() => {
          // on confirm 确认
          let item = [];
          localStorage.setItem("search", JSON.stringify(item));
          this.historyData = item;
        })
        .catch(() => {
          // on cancel  取消
        });
    },
  },
  created() {
    this.$emit("getnavs-btn", false);
    let search = JSON.parse(localStorage.getItem("search"));
    if (search) {
      search.forEach((item) => {
        this.historyData.push(item);
      });
    }

    getRecommendRank().then((data) => {
      this.hotList = data.recommendRank.like;
    });
  },
};
</script>

<style lang='less'>
.search {
  position: relative;
  .title {
    position: fixed;
    width: 100%;
    min-height: 40px;
    display: flex;
    align-items: baseline;
    background-color: #fff;
    z-index: 1;
    i {
      flex: 0 0 40px;
      text-align: center;
      line-height: 40px;
      font-size: 22px;
    }
    .story {
      flex: 1;
      padding: 10px 0;
      position: relative;
      input {
        width: 270px;
        height: 30px;
        background-color: #f6f7f9;
        border: 0;
        padding-left: 30px;
        box-sizing: border-box;
      }
      i {
        position: absolute;
        top: 5px;
        left: 4px;
        line-height: 40px;
        color: #ddd;
      }
    }
    .icon {
      flex: 0 0 50px;
      line-height: 40px;
    }
  }
  .searchContent {
    padding-top: 50px;
    .history {
      padding: 15px;
      .searchHead {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        margin-bottom: 8px;
        h1 {
          font-size: 16px;
          font-weight: 600;
        }
      }
      ul {
        display: flex;
        flex-wrap: wrap;
        li {
          font-size: 12px;
          background-color: #ddd;
          padding: 8px 10px;
          border-radius: 15px;
          margin-right: 8px;
          margin-top: 8px;
        }
      }
    }
    .hot {
      padding: 15px;
      h1 {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 8px;
      }
      .hotWord {
        display: flex;
        flex-wrap: wrap;
        .fire {
          color: #dc2b1d;
        }
        .item {
          font-size: 12px;
          background-color: #ddd;
          padding: 8px 10px;
          border-radius: 15px;
          margin-right: 8px;
          margin-top: 8px;
          line-height: 18px;
        }
      }
    }
    .hotBooks {
      padding: 0 15px;
      h1 {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 8px;
      }
      ul {
        padding-top: 15px;
        li {
          display: flex;
          margin-bottom: 8px;
          h1 {
            width: 20px;
            text-align: center;
            font-size: 16px;
            line-height: 55px;
            margin-right: 6px;
          }
          .hotText {
            flex: 1;
            display: flex;
            .img {
              margin-right: 8px;
              img {
                width: 40px;
                height: 55px;
                border-radius: 6px;
              }
            }
            p {
              flex: 1;
            }
          }
        }
      }
    }
  }
  .searchKeyword {
    width: 100%;
    height: 100vh;
    ul {
      padding-top: 50px;
      li {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        padding: 10px;
        margin-bottom: 1px;
        background-color: #fff;
        i {
          font-size: 20px;
        }
        .item {
          display: flex;
          justify-content: space-between;
          align-items: baseline;
          padding: 4px 10px;
          p {
            width: 300px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
      }
    }
  }
  .searchRes {
    padding-top: 50px;
    ul {
      padding: 0 20px;
      background-color: #fff;
      li {
        display: flex;
        padding: 10px 0;
        .img {
          margin-right: 10px;
          img {
            width: 70px;
            height: 90px;
            border-radius: 6px;
          }
        }
        .content {
          flex: 1;
          color: #999;
          h1 {
            font-size: 16px;
            color: #333;
            font-weight: 600;
            width: 250px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            margin-bottom: 10px;
          }
          p {
            width: 250px;
            font-size: 14px;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
            line-height: 20px;
          }
          span {
            display: block;
            margin-top: 6px;
            font-size: 12px;
          }
        }
      }
    }
  }
}
</style>